<template>
	<view class="container">
		<div class="header">
			<view class="search-type" @click="showSelect=true">{{searchType}}<u-icon name="arrow-down-fill"></u-icon>
			</view>
			<u-search placeholder="请输入搜索条件" v-model="keyword" @search="search" @custom="search"></u-search>
		</div>
		<view class="recordList">

			<view class="team_info" v-for="item in teamList" :key="item.uid">
				<view class="team_box">
					<u-avatar :src="item.head_img" :size="100"></u-avatar>
					<view class="flex1">
						<view class="user_info">
							<view class="nick_name u-line-1 flex1" style="font-size: 25rpx;">
								<view class="nickName"><text style="font-size: 20rpx;">昵称：</text>{{item.name}}</view>
								<view style="color: #fff;"><text
										style="font-size: 20rpx;">客户编码：</text>{{item.spread_code}}</view>
								<view style="color: #fff;"><text style="font-size: 20rpx;">手机号：</text>{{item.phone}}
								</view>
							</view>
							<div>
								<view class="user_level">
									<image class="levelIcon"
										src="https://yunxingzhan.oss-cn-hangzhou.aliyuncs.com/miniprogram/images/images/level.png">
									</image>
									<view class="level_txt">{{item.level_name}}</view>
								</view>
								<div class="price">采购价:￥{{item.goods_price}}</div>
							</div>

							<view class="team_time">
								<view class="btn" @click="showTransfer(item.spread_code)">划拨</view>
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>
		<u-loadmore :status="status" />
		<u-select v-model="showSelect" :list="list" @confirm="confirm"></u-select>
		<u-modal v-model="transferShow" title="划拨库存" :show-cancel-button="true" confirm-text="划拨"
			confirm-color="#20cbd4" @confirm="transfer" @cancel="cancelTransfer">
			<view class="slot-content">
				<u-field class="contentItem" v-model="spread_code" :disabled="true" label="客户编码"
					placeholder="请填写客户编码"></u-field>
				<u-field class="contentItem" v-model="transferPrice" :disabled="true" type="text" label="单价"
					placeholder=" "></u-field>
				<u-field class="contentItem" v-model="num" type="number" label="数量" placeholder="请填写数量"></u-field>
				<u-field class="contentItem" v-model="calcPrice" :disabled="true" label="总金额"
					placeholder="总金额"></u-field>
			</view>
		</u-modal>

		<u-modal v-model="showConfirm" title="确定要划拨吗？" :show-cancel-button="true" @cancel="confirmCancel"
			@confirm="confirmTransfer">
			<view class="slot-content">
				<u-field class="contentItem" v-model="spread_code" :disabled="true" label="客户编码"
					placeholder="请填写客户编码"></u-field>
				<u-field class="contentItem" v-model="transferPrice" :disabled="true" label="单价"
					placeholder="单价"></u-field>
				<u-field class="contentItem" v-model="num" :disabled="true" label="数量" placeholder="数量"></u-field>
				<u-field class="contentItem" v-model="totalPrice" :disabled="true" label="总金额"
					placeholder="总金额"></u-field>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		stockTransfer,
		stockCheckPrice,
		stockTeamList
	} from "@/common/api.js";
	export default {
		components: {},
		data() {
			return {
				showSelect: false,
				list: [{
						value: '1',
						label: '手机号'
					},
					{
						value: '2',
						label: '客户编码'
					}
				],
				searchType: '手机号',
				searchTypeValue: 1,
				showConfirm: false,
				transferShow: false,
				transferPrice: '',
				totalPrice: '',
				calcPrice: '￥0.00',
				transferGoodsPrice: '',
				page: 1,
				num: '',
				msg: '',
				status: 'loadmore',
				spread_code: '',
				keyword: '',
				teamList: []
			};
		},
		onLoad() {
			this.selectOrder()
		},
		onReachBottom() {
			this.selectOrder()
		},
		watch: {
			num(newVal, oldVal) {
				let price = this.transferGoodsPrice * newVal
				price = price.toFixed(2)
				this.calcPrice = '￥' + price
			}
		},
		methods: {
			search() {
				this.$set(this, 'teamList', [])
				this.page = 1
				this.status = 'loadmore'
				this.selectOrder()
			},
			checkPrice() {
				if (this.spread_code && this.spread_code.length > 6) {
					stockCheckPrice(this, {
						spread_code: this.spread_code
					}, (res) => {
						this.transferGoodsPrice = res
						this.transferPrice = '￥' + res
					});
				}
			},
			cancelTransfer() {
				this.spread_code = ''
				this.transferPrice = ''
				this.transferGoodsPrice = ''
				this.num = ''
			},
			showTransfer(spread_code) {
				this.transferShow = true
				this.spread_code = spread_code
				this.calcPrice = '￥0.00'
				this.checkPrice()
			},
			transfer() {
				let price = this.transferGoodsPrice * this.num
				price = price.toFixed(2)
				this.totalPrice = '￥' + price
				//let msg = '客户编码：' + this.spread_code + '，划拨数量：' + this.num + '，总价：￥' + price
				//this.msg = msg
				this.showConfirm = true
			},
			selectOrder() {
				if (this.status !== 'loadmore') {
					return
				}
				this.status = 'loading'
				stockTeamList(this, {
					page: this.page,
					type: this.searchTypeValue,
					keyword: this.keyword
				}, (res) => {
					this.$set(this, 'teamList', [...this.teamList, ...res])
					this.page++
					this.status = 'loadmore'
					if (res.length === 0) {
						this.status = 'nomore'
					}
				})
			},
			confirmCancel() {

			},
			confirm(res) {
				let v = res[0]
				this.searchTypeValue = v.value
				this.searchType = v.label
			},
			confirmTransfer() {
				stockTransfer(this, {
					spread_code: this.spread_code,
					num: this.num
				}, (res) => {
					this.spread_code = ''
					this.num = ''
					this.transferPrice = ''
					this.transferGoodsPrice = ''
					this.$u.toast('划拨成功');
				})
			},
			copyText(spread_code) {
				uni.setClipboardData({
					data: spread_code
				})
			},
		}
	};
</script>
<style lang="scss">
	page {
		font-family: PingFang SC;
		/* #ifndef H5 */
		background: #20cbd4;
		/* #endif */
		/* #ifdef H5 */
		background: #20cbd4;
		/* #endif */
	}

	.header {
		padding: 20rpx;
		border-radius: 15rpx;
		color: #13797f;
		font-size: 35rpx;
		font-weight: 650;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-type {
		font-size: 26rpx;
		color: #FFFFFF;
		margin-right: 10rpx;
	}

	.team_info {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 120rpx;
		border-radius: 50rpx;
		background: rgba(19, 121, 127, .2);
		padding: 0rpx 10rpx;
		margin: 30rpx 20rpx;
	}

	.team_box {
		display: flex;
		align-items: center;
		width: 100%;
	}

	.flex1 {
		flex: 1;
	}

	.user_info {
		display: flex;
		margin-left: 20rpx;
	}

	.nick_name {
		color: #FFFFFF;
		margin-right: 10rpx;
		font-size: 30rpx;
		line-height: 40rpx;
	}

	.user_level {
		height: 40rpx;
		background: linear-gradient(90deg, #fbbf48, #fff) no-repeat;
		;
		border-radius: 20px;
		display: flex;
		align-items: center;
		padding-right: 20rpx;
		margin-top: 20rpx;
	}

	.levelIcon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 10rpx;
		position: relative;
		left: -5rpx;
	}

	.level_txt {
		text-align: center;
		font-size: 20rpx;
		color: #20CBD4;
	}

	.user_remark {
		font-size: 22rpx;
		margin-left: 20rpx;
		color: #13797f;
	}

	.team_time {
		color: #f6f6f6;
		font-size: 28rpx;
		text-align: center;
		margin-left: 20rpx;
		width: 80rpx;
		display: flex;
		align-items: center;
	}

	.price {
		font-size: 24rpx;
		color: #FFFFFF;
		margin-top: 10rpx;
		text-align: center;
	}

	.nickName {
		width: 260rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.copy {
		margin-left: 30rpx;
	}
</style>